package org.exeed.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.exeed.dao.PageHistoryMapper;
import org.exeed.dao.PageMapper;
import org.exeed.dao.PageSketchMapper;
import org.exeed.entity.PageEntity;
import org.exeed.entity.PageHistoryEntity;
import org.exeed.entity.PageSketchEntity;
import org.exeed.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PageService {

    @Autowired
    private PageMapper pageMapper;

    @Autowired
    private PageHistoryMapper pageHistoryMapper;

    public Map selectList(Integer page, Integer size, String key, String type, Integer isPublish) {

        Page<PageEntity> iPage = new Page<>(page, size);
        QueryWrapper<PageEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "path", "type", "title", "des", "thumbnail_url", "publish_time", "update_time", "is_publish", "article_type", "pdf_resource_url");
        if (key != null && !key.equals("")) {
            queryWrapper.like("title", key);
        }
        if (isPublish != null) {
            queryWrapper.eq("is_publish", isPublish);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("update_time");
        IPage<PageEntity> list = pageMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

    public void publish(PageSketchEntity entity) {
        PageEntity page = new PageEntity();
        page.copy(entity);
        page.setUpdateTime(DateUtil.current());
        if (pageMapper.exists(new QueryWrapper<PageEntity>().eq("id", entity.getId()))) {
            pageMapper.updateById(page);
        } else {
            pageMapper.insert(page);
        }

        // 保存历史
        PageHistoryEntity pageHistoryEntity = new PageHistoryEntity().copy(entity);
        pageHistoryEntity.setId(JWTUtils.createId());
        pageHistoryEntity.setUpdateTime(DateUtil.current());
        pageHistoryMapper.insert(pageHistoryEntity);
    }


    public PageEntity selectOneByType(String type) {

        QueryWrapper<PageEntity> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("update_time");
        PageEntity page = pageMapper.selectOne(queryWrapper);
        return page;
    }


    public void unpublish(PageSketchEntity entity) {
        pageMapper.deleteById(entity.getId());
    }

    public PageEntity selectById(Long id) {
        return pageMapper.selectById(id);
    }

}
